﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;
namespace ERP.Khachhang
{
    public partial class viewERP_Khachhang : DevExpress.XtraEditors.XtraForm
    {
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public int flag = 0;
        public int count = 0;
        //public cs_erp_khohangInfo kho = new cs_erp_khohangInfo();
        public cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
        public cs_erp_khachhangInfo khachhang = new cs_erp_khachhangInfo();
        public viewERP_Khachhang()
        {
            InitializeComponent();
        }

        private void btnKhongluu_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }

        private void viewERP_Khachhang_Load(object sender, EventArgs e)
        {
            if (this.flag == 2)
            {
                this.khachhang.Get_Info();
                this.Load_Info();
            }
            this.txtHoten.Focus();
            this.CheckUser();
        }

        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                }
                if (userright.updatedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }
        private void Load_Info()
        {
            this.txtHoten.Text = this.khachhang.tenkhachhang;
            this.txtDiachi.Text = this.khachhang.diachi;
            this.txtDienthoai.Text = this.khachhang.dienthoai;
            this.txtGhichu.Text = this.khachhang.email;
            this.txtNgaysinh.Text = this.khachhang.ngaysinh.ToShortDateString();
        }
        private void Save_Info()
        {
            this.khachhang.tenkhachhang = this.txtHoten.Text.Trim();
            this.khachhang.diachi = this.txtDiachi.Text.Trim();
            this.khachhang.dienthoai = this.txtDienthoai.Text.Trim();
            this.khachhang.email = this.txtGhichu.Text.Trim();
            this.khachhang.ngaysinh = Convert.ToDateTime(this.txtNgaysinh.Text.Trim());
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            this.Save_Info();
            if (this.flag == 1)
            {
                this.khachhang.makh = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
                    + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()
                    + DateTime.Now.Second.ToString() + "-[" + this.user.username + "]";
            }
            if (this.khachhang.tenkhachhang == "" || this.khachhang.diachi == "")
            {
                XtraMessageBox.Show("Thông tin khách hàng chưa được nhập đầy đủ!", "Thông báo...");
            }
            else
            {
                this.khachhang.ExcuteProcedure(this.flag);
                XtraMessageBox.Show("Lưu thông tin khách hàng thành công!", "Thông báo...");
                if (this.count == 1)//cho phep chon truc tiep
                {
                    this.khachhang.Get_Info_by_MAKH();
                    this.count = 2;
                    this.Close();
                }
                this.Close();
            }
            
        }
    }
}